package com.kingwang.training.designpattern.srp.service.impl;

import com.kingwang.training.designpattern.srp.bo.srp.DataChannel;
import com.kingwang.training.designpattern.srp.bo.srp.OpticalModem;
import com.kingwang.training.designpattern.srp.service.DataTransferService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DataTransferServiceImpl implements DataTransferService {
    @Override
    public void sendData(Byte[] data) {
        DataChannel dataChannel = getDataChannel();

        for (Byte item : data) {
            dataChannel.send(item);
        }
    }

    @Override
    public Byte[] receiveData() {
        List<Byte> buffer = new ArrayList<>(1024);

        DataChannel dataChannel = getDataChannel();

        for (int i = 0; i < 1024; ++i) {
            buffer.add(dataChannel.receive());
        }

        return buffer.toArray(new Byte[0]);
    }

    private DataChannel getDataChannel() {
        return new OpticalModem();
    }
}
